<?php

declare(strict_types=1);

namespace app\api\controller;

use app\common\model\Assets;
use app\common\model\Account;
use app\common\model\Relation;
use app\common\tool\Result;
use think\exception\ValidateException;
use think\facade\View;
use app\common\model\User as UserModel;

class User extends Base
{
    /**
     *用户资料
     */
    public function userInfo()
    {
        try {
            $assets = Assets::getFields(3);
            $atitles = [];
            foreach ($assets as $k => $v) {
                $atitle['money'] = $this->user->assets->$k;
                $atitle['title'] = $v;
                $atitle['type'] = $k;
                $atitles[] = $atitle;
            }
            $data = [
                'id' => $this->user->id,
                'code' => $this->user->code,
                'nickname' => $this->user->nickname,
                'photo' => $this->user->photo,
                'mobile' => substr_replace($this->user->mobile, '****', 3, 4),
                'level_name' => $this->user->level->title ?? '',
                'team_name' => $this->user->team->title ?? '',
                'USDT_BSC20' => $this->user->payment_url_1 ?? '',
                'USDT_BEP20' => $this->user->payment_url_2 ?? '',
                'assets' => $atitles,
            ];
            return Result::success('获取成功！', $data);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 修改个人资料
     */
    public function edit()
    {
        try {
            $param = $this->request->post(['nickname', 'photo']);
            UserModel::update($param, ['id' => $this->user->id]);
            return Result::success('已修改');
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 我的团队
     */
    public function myTaem()
    {
        try {
            $page = $this->request->param('page');
            $map = ['uid' => $this->user->id, 'algebra' => 1];
            $list = Relation::getPageList($map, 'id,uid,new_uid,algebra', $page, 20, false, ['fresh']);
            foreach ($list['data'] as &$v) {
                $v->fresh->mobile = substr_replace($v->fresh->mobile, '****', 3, 4);
            }
            $team = Relation::where('uid', $this->user->id)->count();
            $data['list'] = $list['data'];
            $data['team'] = $team;
            $data['team_total'] = $this->user->team_total;
            return Result::success('获取成功！', $data);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 积分明细
     */
    public function getAccountList()
    {
        try {
            $param = $this->request->get();
            $key = $param['type'];
            $map = ['type' => $key, 'uid' => $this->user->id];
            $list =Account::getPageList($map, 'id,uid,type,intro,amount,fx,create_time', $param['page'], 20, false, ['take']);
            $data['money'] = $this->user->assets->$key;
            $data['list'] = $list['data'];
            return Result::success('获取成功！',$data);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 修改密码
     */
    public function editPassword()
    {
        $param = $this->request->post();
        try {
            $this->validate($param, 'Security');
            return Result::fastReturn(UserModel::security($param, $this->user));
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        }catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     * 修改收款方式
     */
    public function savePayment()
    {
        $param = $this->request->post();
        try {
            $this->validate($param, 'Payment');
            return Result::fastReturn(UserModel::savePayment($param, $this->user));
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        }catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
}
